package adapletplugin.dialogs;

import java.util.List;

import objects.IHasAttributes;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import sections.ISection.Section;
import util.MatrixUtils;
import adapletplugin.Activator;

public class NewAttributeDialog extends TitleAreaDialog
{
	private String fAttribute;
	private String fColumn;

	private Combo fAttributeText;
	private Text fColumnText;
	
	private Section fSection;
	private String fTypeName;

	public NewAttributeDialog(Shell parentShell, Section section, String typeName)
	{
		super(parentShell);

		fAttribute = "";
		fColumn = "";
		
		fSection = section;
		fTypeName = typeName;
	}

	@Override
	public void create()
	{
		super.create();

		setTitle("New Attribute");
		setMessage("Enter the necessary information to map the new attribute.");
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	@Override
	protected Control createDialogArea(Composite parent)
	{
		parent.setLayout(new GridLayout());

		Composite body = new Composite(parent, SWT.NULL);
		body.setLayout(new GridLayout(2, false));
		body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label label = new Label(body, SWT.NULL);
		label.setText("Attribute Name:");

		fAttributeText = new Combo(body, SWT.BORDER);
		fAttributeText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fAttributeText.addModifyListener(createModifyListener());
		
		populateComboData(fAttributeText);

		label = new Label(body, SWT.NULL);
		label.setText("Table Column:");

		fColumnText = new Text(body, SWT.BORDER);
		fColumnText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fColumnText.addModifyListener(createModifyListener());

		return parent;
	}

	private void populateComboData(Combo fAttributeText2)
	{
		List<String> attrList = null;
		
		switch(fSection)
		{
		case TYPE:
			attrList = MatrixUtils.getTypeAttributes(fTypeName);
			break;
		case REL:
		case REL_TO_REL:
			attrList = MatrixUtils.getRelationshipAttributes(fTypeName);
			break;
		case INTERFACE:
			attrList = MatrixUtils.getInterfaceAttributes(fTypeName);
			break;
		default:
			break;
		}
		
		IHasAttributes hasAttributes = null;
		switch(fSection)
		{
		case TYPE:
			hasAttributes = Activator.getDefault().getCurrentProject().getTypeSection().getType(fTypeName);
			break;
		case REL:
			hasAttributes = Activator.getDefault().getCurrentProject().getRelationshipSection().getRelationship(fTypeName);
			break;
		case REL_TO_REL:
			hasAttributes = Activator.getDefault().getCurrentProject().getRelToRelSection().getRelationship(fTypeName);
			break;
		case INTERFACE:
			hasAttributes = Activator.getDefault().getCurrentProject().getInterfaceSection().getInterface(fTypeName);
			break;
		default:
			break;
		}
		
		if(attrList != null)
		{
			for(String curAttr : attrList)
			{
				if(hasAttributes.getAttributeColumn(curAttr) == null)
				{
					fAttributeText.add(curAttr);
				}
			}
		}
		
	}

	private ModifyListener createModifyListener()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (fAttributeText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if (fColumnText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else
				{
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		};
	}
	
	@Override
	protected void okPressed()
	{
		fAttribute = fAttributeText.getText();
		fColumn = fColumnText.getText();
		
		super.okPressed();
	}
	
	public String getAttribute()
	{
		return fAttribute;
	}
	
	public String getColumn()
	{
		return fColumn;
	}

}
